package com.itany.corejava.day11;

import java.util.Scanner;

public class TestException {


    public static void main(String[] args) {
//        f1();

//        f2();


//        System.out.println(f3());

        Scanner sc=new Scanner(System.in);
        System.out.println("用户登录");
        System.out.println("用户名：");
        String username=sc.nextLine();
        System.out.println("密码：");
        String password=sc.nextLine();

        try {
            login(username,password);
            System.out.println("登录成功");
        } catch (UserNotFoundException e) {
            System.out.println(e.getMessage());
        } catch (UserDisabledException e) {
            System.out.println(e.getMessage());
        }


    }




    public static void f1() throws Exception {
//        int i=1/0;

//        Exception e=new Exception("产生了Exception");
//
//        throw e;


        throw new Exception("产生了Exception");

    }


    public static void f2(){
        try {
//            int i=1/0;
            System.out.println("TestException.f2");
            throw new Exception("产生了Exception");
        }catch (ArithmeticException e){
            System.out.println("除0异常处理代码");
        }catch (Exception e){
//            System.out.println(e.getMessage());
//            System.out.println(e);
//            e.printStackTrace();
            System.out.println("异常处理代码");
        }


        System.out.println("try之后的代码");
    }

    public static int f3(){
        try{
            System.out.println("try");
//            int i=1/0;
            return 1;
        }catch (Exception e){
            System.out.println("catch");
            return 2;
        }finally{
            System.out.println("finally");
            return 3;
        }
    }

    public static void login(String username,String password) throws UserNotFoundException, UserDisabledException {

        if("abc".equals(username)&&"123".equals(password)){
            return;
        }

        if("aaa".equals(username)&&"111".equals(password)){
            throw new UserDisabledException("用户已被禁用");
        }

        throw new UserNotFoundException("用户名或密码错误");

    }





}
